<?php

namespace app\admin\service;

use app\admin\model\Article as ArticleModel;
use think\facade\Db;

class ArticleService
{
    protected $model;
    protected $categoryService;

    public function __construct(){
        $this->model = app()->make(ArticleModel::class);
        $this->categoryService = app()->make(CategoryService::class);
    }

    /**
     * @param $where
     * @param $pageSize
     * @return ArticleModel[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function queryListByPage($where, $pageSize = 15)
    {
        $list = $this->model->field('id, title, cid, created_time')->order('created_time desc')->paginate($pageSize);
        if (empty($list)) {
            return array();
        }
        $cidArr = array_column($list->toArray()['data'], 'cid');
        $categoryList = $this->categoryService->queryListByIdArr($cidArr);
        $categoryList = array_change_key($categoryList->toArray(), 'id');
        foreach ($list as $key=>$item) {
            if (empty($categoryList[$item['cid']])) {
                continue;
            }
            $category = $categoryList[$item['cid']];
            $item['categoryName'] = $category['name'];
            $list[$key] = $item;
        }
        return $list;
    }

    /**
     * @param int $id
     * @return ArticleModel|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function queryDetail(int $id){
        $article = $this->model->where('id', $id)->find();
        $article['content'] = str_replace("'","\"", $article['content']);
        $article['tags'] = $this->queryTagsByIds(array($id))->toArray();
        return $article;
    }
    public function save(array $data): int
    {
        //print_r($data);
        $result = null;
        if (empty($data['id'])) {
            $this->model->insert($data);
            return $this->model->getLastInsID();
        } else {
            $this->model->where('id', $data['id'])->save($data);
            return $data['id'];
        }
    }

    public function delete(int $id): bool
    {
        return $this->model->where('id', $id)->delete();
    }

    /**
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function queryCount(): int{
        return $this->model->count();
    }

    /**
     * 根据条件查询标签
     *
     * @param $idArr
     * @return array|\think\Collection|Db[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function queryTagsByIds($idArr) {
        $list = Db::table("pre_tag_article")->alias("ta")->join('pre_tag t','ta.tid=t.id')->whereIn('ta.aid',$idArr)->field("ta.aid,t.*")->select();
        if (empty($list)) {
            return array();
        }
        return $list;
    }

}